package com.java.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.print.DocFlavor.STRING;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.java.model.*;
import com.java.service.*;
import com.java.utils.CommonUtils;
import com.java.utils.JsonBody;
import com.java.utils.PageList;
import com.java.utils.ResponseUtil;
import com.sun.corba.se.spi.protocol.RequestDispatcherRegistry;;

@RestController
@RequestMapping("/client")
public class ClientController {
	private ClientService clientService;

	public ClientService getClientService() {
		return clientService;
	}

	@Autowired
	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	
	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("reg")
	public JsonBody reg(@RequestBody Client c) {
		if(clientService.Login(c.getLogin())!=null)
			return Util.SetMap("当前用户名已存在",false);
		if(clientService.Add(c)>0)
			return Util.SetMap("注册成功！",true);
		else
			return Util.SetMap("注册失败",false);
	}
	
	@CrossOrigin(origins = "*")
	@RequestMapping("/edit")
	@ResponseBody
	public JsonBody edit(@RequestBody Client c) {
		if(clientService.Edit(c)>0)
			return Util.SetMap("资料修改成功",true);
		else
			return Util.SetMap("资料修改失败",false);
	}
	
	@CrossOrigin(origins = "*")
	@RequestMapping("/pwd")
	@ResponseBody
	public JsonBody pwd(@RequestBody Client c) {
		int r=clientService.EditPass(c);
		if(r>0)
			return Util.SetMap("修改成功,请重新登陆",true);
		else
			return Util.SetMap("密码修改失败",false);
	}

	@CrossOrigin(origins = "*")
	@RequestMapping("/list")
	@ResponseBody
	public JsonBody  Get(Client c) {
		return Util.SetData(clientService.Get(c));
		
	}
	
	@CrossOrigin(origins = "*")
	@RequestMapping("/getbyid")
	@ResponseBody
	public JsonBody getbyid(int id) {
		return Util.SetData(clientService.GetByID(id), "");
	}
	
	
	@CrossOrigin(origins = "*")
	@RequestMapping("del")
	@ResponseBody
	public JsonBody Del(int id) {
			if(clientService.Del(id)>0)
				return Util.SetMap("删除成功！", true);
			else
				return Util.SetMap("删除失败", false);
	}
	
}